﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
#if false
    public class TextureReferenceHolder : MonoBehaviour
    {
        private TextureReference sr;
        public void Set(TextureReference sr)
        {
            this.sr = sr;
        }
        public TextureReference Get()
        {
            return sr;
        }
    }
#else
    [DisallowMultipleComponent]
    public class TextureReferenceHolder : MonoBehaviour
    {
        private Texture texture;
        private bool isRelease;


        public void SetTexture(Texture texture)
        {
            if (this.texture == texture) return;
            if (this.texture != null)
            {
                this.Release();
            }

            this.texture = texture;
            Retain();
        }
        private void Retain()
        {
            if (this.texture != null && !this.isRelease)
            {
                this.isRelease = TexturePool.Instance.Retain(this.texture);
            }
        }

        private void Release()
        {
            if (this.isRelease && this.texture != null)
            {
                this.isRelease = false;
                TexturePool.Instance.Free(this.texture, false);
                this.texture = null;
            }
        }


        private void OnDestroy()
        {
            Release();
        }


    }



#endif
}
